home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / roids / widget.c < prev   
Encoding:
C/C++ Source or Header  |  1995-05-03  |  6.8 KB  |  242 lines

  1. /*
  2.  * Copyright 1989 Digital Equipment Corporation
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted,
  6.  * provided that the above copyright notice appear in all copies and that
  7.  * both that copyright notice and this permission notice appear in
  8.  * supporting documentation, and that the name of Digital Equipment
  9.  * Corporation not be used in advertising or publicity pertaining to
  10.  * distribution of the software without specific, written prior
  11.  * permission.  Digital Equipment Corporation makes no representations
  12.  * about the suitability of this software for any purpose.  It is
  13.  * provided "as is" without express or implied warranty.
  14.  *
  15.  * DIGITAL EQUIPMENT CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16.  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17.  * FITNESS, IN NO EVENT SHALL DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR
  18.  * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20.  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Terry Weissman
  24.  *          weissman@decwrl.dec.com
  25.  */
  26.  
  27. /* widget.c -- handle things that make the playing field behave as a widget. */
  28.  
  29. #include "roids.h"
  30.  
  31. void HandleFocus();
  32. void HandleStruct();
  33.  
  34. static XtActionsRec actions[] = {
  35.     {"grab-focus",    (XtActionProc) TakeFocus},
  36.     {"rotate-left",    RotateLeft},
  37.     {"rotate-right",    RotateRight},
  38.     {"rotate-off",    RotateOff},
  39.     {"thrust-on",    ThrustOn},
  40.     {"thrust-off",    ThrustOff},
  41.     {"rotate-to-point",    RotateToPoint},
  42.     {"stop-rotate",    StopRotateToPoint},
  43.     {"rotate-moved",    RotateMouseMoved},
  44.     {"fire",        Fire},
  45.     {"quit",        Quit},
  46. };
  47.  
  48. static char defaultTranslation[] =
  49.     "<Btn1Down>:    grab-focus() rotate-to-point()\n\
  50.      Button1<PtrMoved>:    rotate-moved()\n\
  51.      <Btn1Up>:        stop-rotate()\n\
  52.      <Btn3Down>:    thrust-on()\n\
  53.      <Btn3Up>:        thrust-off()\n\
  54.      <Btn2Down>:    fire()\n\
  55.      <KeyDown>z:    rotate-left()\n\
  56.      <KeyUp>z:        rotate-off()\n\
  57.      <KeyDown>x:    rotate-right()\n\
  58.      <KeyUp>x:        rotate-off()\n\
  59.      <KeyDown>\\,:    thrust-on()\n\
  60.      <KeyUp>\\,:    thrust-off()\n\
  61.      <KeyDown>.:    fire()\n\
  62.      <KeyDown>q:    quit()";
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. static void ClassInitialize() {}
  70.  
  71. static void Initialize(request, w)
  72. Widget request, w;
  73. {
  74.     gamewidget = (RoidsWidget) w;
  75.     XtAddEventHandler(toplevel, (EventMask) EnterWindowMask | LeaveWindowMask, 
  76.               FALSE, HandleFocus, (Opaque) NULL);
  77.     XtAddEventHandler(toplevel, (EventMask) StructureNotifyMask, FALSE,
  78.               HandleStruct, (Opaque) NULL);
  79. }
  80.  
  81.  
  82. static void Realize(w, valueMask, attributes)
  83. Widget w;
  84. Mask *valueMask;
  85. XSetWindowAttributes *attributes;
  86. {
  87.     int i;
  88.     XGCValues values;
  89.  
  90. /*
  91.  * Safety check.
  92.  */
  93.  
  94.     if (w->core.width == 0) w->core.width = 1;
  95.     if (w->core.height == 0) w->core.height = 1;
  96.  
  97.     XtCreateWindow(w, (unsigned int) InputOutput, (Visual *) CopyFromParent,
  98.            *valueMask, attributes);
  99.     gamewidth = w->core.width;
  100.     gameheight = w->core.height;
  101.     gamewindow = w->core.window;
  102.     values.foreground = w->core.background_pixel;
  103.     values.background = w->core.background_pixel;
  104.     backgc = XtGetGC(w, (XtGCMask) GCForeground | GCBackground, &values);
  105.     values.foreground = shippixel;
  106.     shipgc = XtGetGC(w, (XtGCMask) GCForeground | GCBackground, &values);
  107.     values.foreground = rockpixel;
  108.     rockgc = XtGetGC(w, (XtGCMask) GCForeground | GCBackground, &values);
  109.     values.foreground = shotpixel;
  110.     shotgc = XtGetGC(w, (XtGCMask) GCForeground | GCBackground, &values);
  111.     InitShip();
  112.     InitRocks();
  113.     InitShot();
  114.     InitScore();
  115. }
  116.  
  117.  
  118. static void HandleExpose(w, event)
  119. Widget w;
  120. XEvent *event;
  121. {
  122.     if (event->xexpose.count) return;
  123.     if (!shipdestroyed)
  124.     PaintShip(shipgc);
  125.     PaintAllRocks();
  126.     PaintAllShots();
  127.     XSync(dpy, 0);
  128. }
  129.  
  130.  
  131. static Boolean wason = TRUE;    /* Whether the user had autorepeat set. */
  132.  
  133. void Quit()
  134. {
  135.     if (wason) XAutoRepeatOn(dpy);
  136.     XCloseDisplay(dpy);
  137.     printf("Score: %d\n", score);
  138.     exit(0);
  139. }
  140.  
  141. static void HandleFocus(w, closure, event)
  142. Widget w;
  143. Opaque closure;
  144. XEvent *event;
  145. {
  146.     XKeyboardState info;
  147.     if (event->type == EnterNotify) {
  148.     XGetKeyboardControl(dpy, &info);
  149.     wason = info.global_auto_repeat;
  150.     XAutoRepeatOff(dpy);
  151.     XSync(dpy, 0);
  152.     if (shiptimerid == NULL)
  153.         shiptimerid = XtAddTimeOut(shipwait, MoveShip, (Opaque) MoveShip);
  154.     if (rocktimerid == NULL)
  155.         rocktimerid = XtAddTimeOut(rockwait, MoveRocks,
  156.                        (Opaque) MoveRocks);
  157.     if (shottimerid == NULL)
  158.         shottimerid = XtAddTimeOut(shotwait, MoveShots,
  159.                        (Opaque) MoveShots);
  160.     } else if (event->type == LeaveNotify) {
  161.     if (wason) XAutoRepeatOn(dpy);
  162.     if (shiptimerid) 
  163.         XtRemoveTimeOut(shiptimerid);
  164.     shiptimerid = NULL;
  165.     if (rocktimerid)
  166.         XtRemoveTimeOut(rocktimerid);
  167.     rocktimerid = NULL;
  168.     if (shottimerid)
  169.         XtRemoveTimeOut(shottimerid);
  170.     shottimerid = NULL;
  171.     }
  172. }
  173.  
  174. static void HandleStruct(w, closure, event)
  175. Widget w;
  176. Opaque closure;
  177. XEvent *event;
  178. {
  179. }
  180.  
  181. static void Destroy() {}
  182.  
  183. static void Resize(w) 
  184. Widget w;
  185. {
  186. /* 
  187.  * Resize the playing field.
  188.  */
  189.     gamewidth = w->core.width;
  190.     gameheight = w->core.height;
  191. }
  192.  
  193. static Boolean SetValues() {}
  194.  
  195. static Boolean TakeFocus()
  196. {
  197.     XSetInputFocus(dpy, gamewindow, RevertToPointerRoot, CurrentTime);
  198.     return TRUE;
  199. }
  200.  
  201. RoidsClassRec roidsClassRec = {
  202.   {
  203. /* core_class fields      */
  204.     /* superclass         */    (WidgetClass) &widgetClassRec,
  205.     /* class_name         */    "Roids",
  206.     /* widget_size        */    sizeof(RoidsRec),
  207.     /* class_initialize   */    ClassInitialize,
  208.     /* class_part_initiali*/    NULL,
  209.     /* class_inited       */    FALSE,
  210.     /* initialize         */    Initialize,
  211.     /* initialize_hook      */    NULL,
  212.     /* realize            */    Realize,
  213.     /* actions            */    actions,
  214.     /* num_actions        */    XtNumber(actions),
  215.     /* resources          */    NULL,
  216.     /* num_resources      */    (Cardinal) 0,
  217.     /* xrm_class          */    NULLQUARK,
  218.     /* compress_motion    */    TRUE,
  219.     /* compress_exposure  */    TRUE,
  220.     /* compress_enterleave*/    TRUE,
  221.     /* visible_interest   */    FALSE,
  222.     /* destroy            */    Destroy,
  223.     /* resize             */    Resize,
  224.     /* expose             */    HandleExpose,
  225.     /* set_values         */    SetValues,
  226.     /* set_values_hook      */    NULL,
  227.     /* set_values_almost  */    NULL,
  228.     /* get_values_hook      */    NULL,
  229.     /* accept_focus       */    TakeFocus,
  230.     /* version          */    XtVersion,
  231.     /* callback_private      */    NULL,
  232.     /* tm_table          */    defaultTranslation,
  233.     /* query_geometry      */    NULL,
  234.     /* display_accelerator*/    NULL,
  235.     /* extension`      */    NULL,
  236.   },{
  237.     /* mumble             */    0       /* Make C compiler happy   */
  238.   }
  239. };
  240.  
  241. WidgetClass roidsWidgetClass = (WidgetClass)&roidsClassRec;
  242.